package ilikethat;
import java.util.*;
import java.sql.*;

public class ChoosePics {
	public static DBConnector db = DBConnector.getInstance(); 
	public static Connection conn = db.getConnection();
	
	


	/*--------------------------------------------------------------------*/
	/*
	 * Game Shit
	 * /*--------------------------------------------------------------------
	 */

	public static String getCategoryByGameID(String gameID)
			throws SQLException {
		Statement stmt = null;
		ResultSet rs = null;
		String output = null;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT category FROM games WHERE gameID='"
					+ gameID + "'");

			
		} finally {
			
			if (rs != null) {
				try {
					while (rs.next()) {
						for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
							output = rs.getString(i);
						}
					}
					rs.close();
					 return output;
				} catch (SQLException sqlEx) { // ignore }

					rs = null;
				}

				if (stmt != null) {
					try {
						stmt.close();
					} catch (SQLException sqlEx) { // ignore }

						stmt = null;
					}
				}

			}
		}
		 return output;
		
	}

	public static String[] getPictures(String category) throws SQLException {
		Statement stmt = null;
		ResultSet rs = null;
		String[] resultArray = new String[4];
		int value1=0, value2=0, value3=0, value4=0;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT picURL FROM cat" + category);

		} finally {
			int TableLength = 0;
				try {
					if(rs != null){
						rs.beforeFirst();
						rs.last();
						TableLength = rs.getRow();
					}

				} catch (SQLException sqlEx) { // ignore }

					rs = null;
				}
				TableLength-=1;
				value1 = (int)(Math.random()*((TableLength-1) + 1))+1;
				value2 = (int)(Math.random()*((TableLength-1) + 1))+1;
				value3 = (int)(Math.random()*((TableLength-1) + 1))+1;
				value4 = (int)(Math.random()*((TableLength-1) + 1))+1;
				
				while(value1 == value2 || value1 == value3 || value1 == value4 || value2 == value3 || value2 == value4 || value3 == value4) {
					value1 = (int)(Math.random()*((TableLength-1) + 1))+1;
					value2 = (int)(Math.random()*((TableLength-1) + 1))+1;
					value3 = (int)(Math.random()*((TableLength-1) + 1))+1;
					value4 = (int)(Math.random()*((TableLength-1) + 1))+1;
				}
				
//				System.out.println(value1 + " -- " + value2 + " -- " + value3 + " -- " + value4);
				
				while(rs.previous()){
					if(value1 == rs.getRow()){
						resultArray[0] = rs.getString(1);
					}			
					if(value2 == rs.getRow()){
						resultArray[1] = rs.getString(1);
					}
					if(value3 == rs.getRow()){
						resultArray[2] = rs.getString(1);
					}
					if(value4 == rs.getRow()){
						resultArray[3] = rs.getString(1);
					}
				
				}
//				for (int i = 0; i < resultArray.length; i++) {
//					System.out.println(resultArray[i]);
//				}
				
			}
		rs.close();
		 return resultArray;
	}
	
	public static boolean checkPicUrl(String category, String url) throws SQLException {
		Statement stmt = null;
		ResultSet rs = null;
		boolean output = false;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT picURL FROM cat" + category
					+ " WHERE picURL='" + url + "'");

		} finally {

			if (rs != null) {
				try {
					while (rs.next()) {
							if (rs.getString(1).equals(url)) {
								 return output=true;
							}
					}
					rs.close();
				} catch (SQLException sqlEx) { // ignore }

					rs = null;
				}

				if (stmt != null) {
					try {
						stmt.close();
					} catch (SQLException sqlEx) { // ignore }

						stmt = null;
					}
				}

			}
		}
		 return output=false;
	}

	
//	public static void main(String[] args) throws SQLException {
//		DBConnector db = DBConnector.getInstance();
//		Connection conn = db.getConnection();	
//		ChoosePics.getPictures("Sport", conn);
//	}
	
//
//		System.out.println(Select.checkPicUrl("Tiere", "moi1n", conn));
//	}

}
